package com.yanqu.road.server.cmd.activity.peakstronghold;

import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongHoldConfig;
import com.yanqu.road.entity.activity.peakstronghold.config.ePeakStrongholdStageType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.PeakStrongholdWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.peakstronghold.PeakStrongholdModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.peakstronghold.PeakStrongholdActivityMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.math.BigInteger;

@Cmd(code = GameProtocol.S_PEAK_STRONGHOLD_SILVER_DONATE_REQ, desc = "银两捐献")
public class PeakStrongholdSilverDonateCmd extends AbstractCommand {

    PeakStrongholdWarProto.PeakStrongholdSilverDonateReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = PeakStrongholdWarProto.PeakStrongholdSilverDonateReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        PeakStrongHoldConfig config = PeakStrongholdActivityMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        if (!PeakStrongholdActivityMgr.canUnionInBattle(player.getUserInfo().getUnionUid())) {
            ret = GameErrorCode.E_PEAK_HOLD_NOT_IN_ELIMINATE;
            return;
        }

        if (config.getStageType() != ePeakStrongholdStageType.Prepare) {
            ret = GameErrorCode.E_PEAK_HOLD_ELIMINATE_NOT_PREPARE;
            return;
        }

        ret = player.getModule(PeakStrongholdModule.class).silverDonate(new BigInteger(reqMsg.getNum()));
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            PeakStrongholdWarProto.PeakStrongholdSilverDonateRespMsg.Builder respMsg = PeakStrongholdWarProto.PeakStrongholdSilverDonateRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(ClientProtocol.U_PEAK_STRONGHOLD_SILVER_DONATE_REQ, respMsg);
        }
    }
}
